package com.company.consumer.controller;

import com.company.api.entity.Goods;
import com.company.api.service.GoodsClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class GoodsConsumerContrller {

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-GOODS";

    @Autowired
    private GoodsClientService goodsClientService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/goods/list")
    public List<Goods> getGoods() {
        return restTemplate.exchange(REST_URL_PREFIX + "/goods/list", HttpMethod.GET,
             null,   new ParameterizedTypeReference<List<Goods>>(){}).getBody();
    }

    @GetMapping("/goods/list/feign")
    public List<Goods> getGoodsByFeign() {
        return goodsClientService.getGoods();
    }
}
